import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class DataFilesHandler {

	private List<File> dataFileNameL;
	private List<User> usersInXml;
	private List<String> dateFolder;
	private List<String> newFileName;
	private String outputPath;
	private String inputDataPath;
	
	public DataFilesHandler(List<User> usersInXml, String outputPath,
			String inputDataPath) {
		super();
		this.usersInXml = usersInXml;
		this.outputPath = outputPath;
		this.inputDataPath = inputDataPath;
	}

	public void processFileNamesAndSave(){
		readDataFiles();
		//read dates into list
		dateFolder= new ArrayList<String>();
		newFileName = new ArrayList<String>();
		for (File oldFile : dataFileNameL) {
			String curFile=oldFile.getName();
			dateFolder.add(getMoveFolder(curFile));
			String curUserName=getUserNameFromFile(curFile);
			String newName=getWithOutExtension(curFile).concat(curUserName).concat(".txt");
			newFileName.add(newName);
			File newFile= new File(outputPath+"\\"+curUserName+"\\"+newName);
			Logger.log(MsgType.MESSAGE, "Saving "+newFile.toString());
			FileUtils.copyAndSaveFilesInCorrectFolders(oldFile, newFile);
		}
	}
	private void readDataFiles(){
		dataFileNameL=FileUtils.readFolderContent(inputDataPath);
	}
	private String getWithOutExtension(String fileName){
		String []underScoreSplits= fileName.split("\\.");
		return underScoreSplits[0];
	}
	private String getMoveFolder(String fileName){
		String []underScoreSplits= fileName.split("[. _]");
		return underScoreSplits[0];
	}
	private String getUserNameFromFile(String fileName){
		//split with underscore
		String []underScoreSplits= fileName.split("[. _]");
		return UserUtil.getUserNameOfId(usersInXml, underScoreSplits[2]);
		
	}
	
}
